<?php

$redis=new Redis();
function is_ie() {
  if (isset($_SERVER['HTTP_USER_AGENT']) &&
      (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)) {
    return true;
  } else {
    return false;
  }
}

function G($key){
    return isset($_GET[$key])?$_GET[$key]:NULL;
}

function P($key){
    return isset($_POST[$key])?$_GET[$key]:NULL;
}
function str_rand($length) {
  $r = '';

  for (; $length > 0; --$length) {
    $r .= chr(rand(32, 126)); // 32 - 126 is the printable ascii range
  }

  return $r;
}
function fixRedisConnection(){
    global $conf,$redis;
        $server=G('server');
        if(is_numeric($server)&&isset($conf['redis'][$server])){
            $r=$conf['redis'][$server];
             redis_init($r['ip'],$r['port']);
        }
}
function is_ajax(){
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH'])||
    $_SERVER['HTTP_X_REQUESTED_WITH']!=='XMLHttpRequest')
    {
        exit('access denied');
    }
//X-Requested-With
//$_SERVER['X-Requested-With']
}
function redis_init($server_ip,$port=6379,$timeout=0){

    global $redis;
//    echo $server_ip,$port,$timeout;
    if(!$redis->connect($server_ip)){
       exit("redis 连接失败");
    }
}
function redis_get_key($key){
    global $redis,$redistypes;
    $arr=array(
    'key'=>$key,
    'value'=>$redis->get($key),
    'type'=>$redistypes[$redis->type($key)],
    'ttl'=>$redis->ttl($key),
//    'l'.$key=>$redis->lGet($key,0),
    );
    return $arr;
}
function redis_get_hash($key){
    global $redis,$redistypes;
    $retArray=array();
    $keys=$redis->hKeys($key);
    $key_info=array(
    'type'=>$redistypes[$redis->type($key)],
    'ttl'=>$redis->ttl($key),
    );
    if(isset($keys)&&count($keys))
    {
       foreach ($keys as $k=>$v) {
          $retArray[]=array(
                           'key'=>$v,
                           'value'=>$redis->hGet($key,$v),
                           );
       }
    }
   return array_merge($key_info,$retArray);
}
function redis_get_list($key){
 global $redis,$redistypes;
    $keys=$redis->lSize($key);
    $retArray=array();
    $key_info=array(
    'type'=>$redistypes[$redis->type($key)],
    'ttl'=>$redis->ttl($key),
    );
    if($keys>0)
    {
       $i=0;
       while($i<$keys)
       {
          $retArray[]= $redis->lGet($key,$i++);
       }
    }
   return array_merge($key_info,$retArray);
}
function redis_get_set($key){
   global $redis,$redistypes;
    $keys=$redis->sMembers($key);
    $key_info=array(
    'type'=>$redistypes[$redis->type($key)],
    'ttl'=>$redis->ttl($key),
    );

    if($keys)
    {
         return array_merge($key_info,$keys);
    }
    return array();

}
function redis_get_zset($key){
    global $redis,$redistypes;
    $key_count=$redis->zSize($key);
    $retArray=$redis->zRange($key,0,$key_count);
     $key_info=array(
    'type'=>$redistypes[$redis->type($key)],
    'ttl'=>$redis->ttl($key),
    );
    //$retArray=array();
    if($key_count>0)
    {
        foreach ($retArray as $k=>&$v) {
           $v=array('value'=>$v,'index'=>$redis->zScore($key,$v));
        }
//       $i=0;
//       while(++$i<$key_count)
//       {
//          $retArray[]= $redis->lGet($key,$i);
//       }
    }
//    pr($retArray);
      return array_merge($key_info,$retArray);
}
function redis_set_key($key,$value){
    global $redis;
    $redis->set($key,$value);
}
function redis_info(){
    global $redis;
    return $redis->info();
}
function redis_delete($key,$type=1,$index=0,$value=NULL,$hkey=NULL)
{
    global $redis;
    $ret=false;
//    echo $type,$key,$value;
    switch ($type)
       {
           case 1:
               $ret=$redis->delete($key);
               break;
           case 4:
               $ret=$redis->sRem($key,$value);
               break;
           case 3:
                $v = str_rand(69);
                $redis->lSet($key, $index, $v);
                $ret=$redis->lRem($key, $v, 1);
               break;
           case 5:
                $ret=$redis->zDelete($key,$value);
               break;
           case 2:
//               echo $key,$hkey;
               $ret=$redis->hDel($key,$hkey);
               break;
           default:
               break;
       }
     //$ret=$redis->delete($key);
     return $ret;
    //return $redis->delete($key);
}
//  Redis::REDIS_STRING    => 'string',
//  Redis::REDIS_SET       => 'set',
//  Redis::REDIS_LIST      => 'list',
//  Redis::REDIS_ZSET      => 'zset',
//  Redis::REDIS_HASH      => 'hash',
function redis_do_update($key,$value,$type,$ttl){
       global $redis;
       $ret=TRUE;
       switch ($type)
       {
           case Redis::REDIS_STRING:
              $ret=$redis->set($key,stripslashes($value));
               break;
           case Redis::REDIS_SET:
               //$ret=$redis->sAdd($key,$value);
               break;
           case Redis::REDIS_LIST:
               //$ret=$redis->lSet($key,0,$value);
               break;
           case Redis::REDIS_ZSET:
                //$ret=$redis->zAdd($key,0,$value);
               break;
           case Redis::REDIS_HASH:
                 //$ret=$redis->hSet($key,$key,$value);
               break;
           default:
               break;
       }
       if($ret)
       {
         if($ttl==-1){
           $redis->persist($key);
         }else{
           $redis->setTimeout($key,$ttl);
         }
       }
       return $ret?$ret:$ret;
}
function redis_do_key_update($key,$type=0,$index=0,$value=NULL,$oldvalue=NULL,$hkey=NULL){
       global $redis;
       $ret=FALSE;
       switch ($type)
       {
           case 1:
              $ret=$redis->set($key,$value);
               break;
           case 2:
               $ret=$redis->hDel($key,$hkey);
               $ret=$redis->hSet($key,$hkey,$value);
               break;
           case 3:
               $ret=$redis->lSet($key,$index,$value);
               break;
           case 4:
               $redis->sRem($key, $oldvalue);
               $ret=$ret=$redis->sAdd($key, $value);
               break;
           case 5:
               //echo $index,"\n",$key,"\n",$value,"\n",$oldvalue;
               $redis->zDelete($key, $oldvalue);
               $ret=$redis->zAdd($key, $index, $value);
               break;
           default:
               break;
       }
       return $ret?$ret:$ret;
}
function redis_do_add($key,$type=0,$index=0,$value=NULL,$hkey=NULL){
 global $redis;
       $ret=FALSE;
       switch ($type)
       {
           case 1:
              $ret=$redis->set($key,$value);
               break;
           case 2:
//               $ret=$redis->hSet($key,$hkey,$value);
               $ret=$redis->sAdd($key,$value);
               break;
           case 3:

               $size=$redis->lSize($key);
               if($size==0){
                $ret=$redis->lPush($key,$value);
               }else{
                if($index<=$size){
                    $ret=$redis->lSet($key,$index,$value);
                }else{
                    $ret=$redis->lPush($key,$value);
                }
               }
               break;
           case 4:
               $ret=$ret=$redis->zAdd($key,$index, $value);
               break;
           case 5:
//               echo $index,"\n",$key,"\n",$value,"\n",$oldvalue;
              if($redis->hGet($key,$hkey))
              {
                 $redis->hDel($key,$hkey);
              }
               $ret=$redis->hSet($key, $hkey, $value);
               break;
           default:
               break;
       }
       return $ret?$ret:$ret;
}
function redis_list($key){
   global $redis,$redistypes;
   $retArray=array();
   $arr=$redis->keys($key.'*');
   if($arr&&count($arr)>0)
   {
       foreach ($arr as $k=>$v) {
           $retArray[]=array(
            'key'=>$v,
            'type'=>$redistypes[$redis->type($v)],
           );
       }
   }
   return $retArray;
}
function pr() {
	$color = dechex ( rand ( 0, 255 ) ) . dechex ( rand ( 0, 255 ) ) . dechex ( rand ( 0, 255 ) );
	if (strlen ( $color ) != 6) {
		$color .= str_repeat ( 0, 6 - strlen ( $color ) );
	}
	echo "<pre style=\"background-color:#$color;\">";
	$arr = func_get_args ();
	foreach ( $arr as $k => $v ) {
		print_r ( $v );
	}
	echo '</pre>';
}
function prd(){
 $color = dechex(rand(100, 200)) . dechex(rand(100, 200)) .
         dechex(rand(100, 200));
        if (strlen($color) != 6) {
            $color .= str_repeat(0, 6 - strlen($color));
        }
        echo "<pre style=\"background-color:#$color;\">";
        $arr = func_get_args();
        foreach ($arr as $k => $v) {
            if (is_object($v)) {
                 print_r($v);
                $ref = new ReflectionClass($v);
                $methods = $ref->getMethods();
                $consts = $ref->getConstants();
                $prosps=$ref->getProperties();
                $statics=$ref->getStaticProperties();
                $name=$ref->getName();
                echo 'name='.$name."\n";
                echo 'consts={';
                if ($consts && is_array($consts)) {
                    foreach ($consts as $k2 => $v2) {
                        if (method_exists($v2, '__toString')) {
                            echo $v2->__toString();
                        }
                    }
                }
                echo "}\n";
                echo "Prosp={\n";
                if ($prosps && is_array($prosps)) {
                    foreach ($prosps as $k3 => $v3) {
                        if (method_exists($v3, '__toString')) {
                            echo $v3->__toString();
                        }
                    }
                }
                echo "}\n";
                echo "static prosp={\n";
               if ($statics && is_array($statics)) {
                    foreach ($statics as $k4 => $v4) {
                        if (method_exists($v4, '__toString')) {
                            echo $v4->__toString();
                        }
                    }
                }
                 echo "}\n";
                 echo "methods={\n";
                if ($methods && is_array($methods)) {
                    foreach ($methods as $k1 => $v1) {
                        if (method_exists($v1, '__toString')) {
                            echo $v1->__toString();
                        }
                    }
                }
                 echo "}\n";
            } else if(is_array($v)){
                print_r($v);
            }else if(is_string($v)){
                print_r($v);
            }
        }
        echo '</pre>';
}
function vd() {
	$color = dechex ( rand ( 0, 255 ) ) . dechex ( rand ( 0, 255 ) ) . dechex ( rand ( 0, 255 ) );
	if (strlen ( $color ) != 6) {
		$color .= str_repeat ( 0, 6 - strlen ( $color ) );
	}
	echo "<pre style=\"background-color:#$color;\">";
	$arr = func_get_args ();
	foreach ( $arr as $k => $v ) {
		var_dump ( $v );
	}
	echo '</pre>';
}